package com.help10000.rms.ui.activitys.mine.address

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.beans.AddressEntity
import com.help10000.rms.R
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.mvp.presenter.AddressListPresenter
import com.help10000.rms.mvp.view.AddressView
import com.help10000.rms.ui.adapter.AddressListAdapter
import com.help10000.rms.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_address.*

class AddressActivity : BaseActivity<AddressListPresenter, AddressView>(), AddressView {
    override fun getlayoutId(): Int {
        return R.layout.activity_address
    }

    override fun initView() {
        swipeLayout.setColorSchemeColors(
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary)
        )
    }

    override fun initListener() {
        swipeLayout.setOnRefreshListener {
            mPresenter.getAddressList()
        }
        addAddressTv.setOnClickListener(DuplicateClickProxy {
            startActivity(Intent(this, AddAddressActivity::class.java))
        })
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun onResume() {
        super.onResume()
        mPresenter.getAddressList()
    }

    override fun showLoading() {
        if (!swipeLayout.isRefreshing)
            super.showLoading()
    }

    override fun success() {

        mPresenter.getAddressList()
    }

    override fun createPresenter(): AddressListPresenter {
        return AddressListPresenter(this)
    }

    override fun onError(mess: String?) {
        super.onError(mess)
        swipeLayout.isRefreshing = false
    }

    override fun onDataSuccess(data: AddressEntity) {
        swipeLayout.isRefreshing = false
        if (data.data.AddrList.isEmpty()) {
            tv_empty.visibility = View.VISIBLE

            recyclerView.adapter = null
            return
        }
        tv_empty.visibility = View.GONE
        recyclerView.apply {
            itemAnimator = null
            layoutManager = LinearLayoutManager(context)
            adapter = AddressListAdapter(context, data.data.AddrList, mPresenter)
        }
    }


}